package com.yiqi.liebang.feature.order.view;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.DateUtil;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.DateTool;
import com.yiqi.liebang.entity.bo.ExpertTopicBo;
import com.yiqi.liebang.entity.bo.TopicOrderInfoBo;


import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imkit.RongIM;

import static com.yiqi.liebang.common.util.DateTool.DEFAULT_NYRSF;

public class TakeTimeActivity extends BaseActivity  {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_show_start_time)
    TextView mTvShowStartTime;
    @BindView(R.id.tv_show_end_time)
    TextView mTvShowEndTime;
    @BindView(R.id.tv_show_position)
    EditText mTvShowPosition;
    @BindView(R.id.tv_show_address)
    EditText mTvShowAddress;
    @BindView(R.id.view_position)
    RelativeLayout mViewPosition;
    @BindView(R.id.view_address)
    RelativeLayout mViewAddress;
    private boolean isOnline;
    private TopicOrderInfoBo id;
    ExpertTopicBo expertTopicBo = null;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_take_time;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        isOnline = this.getIntent().getBooleanExtra("is_online", false);
        id = (TopicOrderInfoBo) this.getIntent().getSerializableExtra("id");
        expertTopicBo = (ExpertTopicBo) this.getIntent().getSerializableExtra("meet");
        if (expertTopicBo != null) {
            mTvShowStartTime.setText(expertTopicBo.getMettingBeginTime());
            mTvShowEndTime.setText(expertTopicBo.getMettingEdnTime());
            mTvShowPosition.setText(expertTopicBo.getMettingAddress());
            mTvShowAddress.setText(expertTopicBo.getDetailedAddress());
        }
        if (isOnline) {
            initToolBar(mToolbar, "预约时间", true, true);
            mViewPosition.setVisibility(View.GONE);
            mViewAddress.setVisibility(View.GONE);
        } else {
            initToolBar(mToolbar, "预约时间地点", true, true);
        }
    }


    private Date mStartDate = null;

    @OnClick({R.id.tv_show_start_time, R.id.tv_show_end_time, R.id.btn_topic_msg, R.id.btn_topic_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_show_start_time:

               TimePickerView pvStart = new com.bigkoo.pickerview.builder.TimePickerBuilder(this,null, new com.bigkoo.pickerview.listener.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        mStartDate = date;
                        mTvShowStartTime.setText(DateTool.date2String(date, DEFAULT_NYRSF));
                    }

                   @Override
                   public void onTimeZhijin(Date date, View v) {

                   }
               })
                        .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                        .setLineSpacingMultiplier(1.8f)
                        .build();
                pvStart.show();
                break;
            case R.id.tv_show_end_time:
                TimePickerView pvEnd = new TimePickerBuilder(this, mStartDate,new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        mTvShowEndTime.setText(DateTool.date2String(date, DEFAULT_NYRSF));
                    }

                    @Override
                    public void onTimeZhijin(Date date, View v) {

                    }
                })
                        .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                        .setLineSpacingMultiplier(1.8f)
                        .setRangDate(DateTool.dataToCalendar(mStartDate),null)
                        .build();

                pvEnd.show();


                break;
            case R.id.btn_topic_msg:
                if (id != null)
                    try {
                        RongIM.getInstance().startPrivateChat(this,
                                id.getTopicFormMap().getTopicUserAccount().getUserUid(),
                                id.getTopicFormMap().getTopicUserAccount().getUserName());
                    } catch (Exception e) {
                        T.showShort(e.getMessage());
                    }

                break;
            case R.id.btn_topic_save:
                toConfrim();
                break;

        }
    }




    private void toConfrim() {


        if (TextUtils.isEmpty(mTvShowStartTime.getText().toString())) {
            T.showShort("请选择开始时间！");
            return;

        }
        if (TextUtils.isEmpty(mTvShowEndTime.getText().toString())) {
            T.showShort("请选择结束时间！");
            return;

        }


        if (DateUtil.getTimeCompareSizew(mTvShowStartTime.getText().toString(), mTvShowEndTime.getText().toString()) != 3) {
            T.showShort("结束时间必须大于开始时间！");
            return;
        }

        if (!isOnline) {
            if (TextUtils.isEmpty(mTvShowPosition.getText().toString())) {
                T.showShort("请输入地点！");
                return;

            }
            if (TextUtils.isEmpty(mTvShowAddress.getText().toString())) {
                T.showShort("请输入详细地址！");
                return;

            }
            expertTopicBo = new ExpertTopicBo(id.getId(), mTvShowStartTime.getText().toString(), mTvShowEndTime.getText().toString(), mTvShowPosition.getText().toString(), mTvShowAddress.getText().toString());
        } else {
            expertTopicBo = new ExpertTopicBo(id.getId(), mTvShowStartTime.getText().toString(), mTvShowEndTime.getText().toString());
        }
        Intent intent = new Intent();
        intent.putExtra("meet", expertTopicBo);
        setResult(2, intent);
        finish();

    }


}
